package com.rentplant.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/sql")
public class jdbcController {

//    自动找jdbc 进行使用
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/register")
    public String register(@RequestBody JSONObject req){
        String username = req.getString("nickname");
        String ava = req.getString("ava");

    try{

        //        sql语句
        String sql = "insert into user (username,avatar) value (?,?)";
        //        使用sql语句,后面写上参数
        jdbcTemplate.update(sql,username,ava);

        return  "success";
    }catch (Exception e){
        return "fail";
    }

    }



    @PostMapping("/login")
    public String login(@RequestBody JSONObject req){
        String username = req.getString("nickname");
        String ava = req.getString("ava");

        try{

            //        sql语句
            String sql = "insert into user (username,avatar) value (?,?)";
            //        使用sql语句,后面写上参数
            jdbcTemplate.update(sql,username,ava);

            return  "success";
        }catch (Exception e){
            return "fail";
        }

    }
}
